<?php

namespace App\Repository\Repositories;

use App\Repository\Models\AdminUser;
use App\Repository\Models\Menu;
use App\Repository\Models\MenuPermission;
use App\Repository\Models\Permission;
use App\Repository\Models\PermissionRole;
use App\Repository\Models\RoleUser;
use App\Repository\Repositories\Interfaces\AdminUserRepository;

/**
 * Class AdminUserRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class AdminUserRepositoryEloquent extends BaseRepository implements AdminUserRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AdminUser::class;
    }


    /**
     *  菜单权限缓存标识
     */
    const USER_MENU_PERMISSIONS_CACHE = "user_menu_permissions_cache";

    /**
     * 操作权限缓存标识
     */
    const USER_ACTION_PERMISSIONS_CACHE = "user_action_permissions_cache";

    /**
     * 根据用户模型获取用户的菜单路由权限
     *
     * @param $user
     *
     * @return array|mixed
     */
    public function getUserMenusPermissionsByUserModel($user)
    {
        $routes = \Cache::get(self::USER_MENU_PERMISSIONS_CACHE . $user->id);

        if(empty($routes)){

            $roles = RoleUser::query()->where('user_id',$user->id)->get()->toArray();

            $permissions = [];
            foreach ($roles as $key => $role) {
                $permissions_role=PermissionRole::query()->where('role_id',$role['role_id'])->get()->toArray();
                if(!empty($permissions_role)){
                    foreach($permissions_role as $k=>$v){
                        $permissions[] = $v['permission_id'];
                    }
                }
            }

            $menus_per = [];
            foreach ($permissions as $k=>$v) {
               $per=Permission::query()->where('id',$v)->get()->toArray();
               if($per[0]['type'] == 'menu'){
                   $menus_per[] = $per[0]['id'];
               }
            }
            $menus=[];
            foreach ($menus_per as $menu) {
                $MenuPermission=MenuPermission::query()->where('permission_id',$menu)->get()->toArray();
                $menus[]=$MenuPermission[0];
            }

            foreach($menus as $k=>$v){
                $menu_arr=Menu::query()->where('id',$v['menu_id'])->get()->toArray();
                $routes[] =$menu_arr[0]['route'];
            }

            if($routes){
                $routes = array_unique($routes);

            }

            \Cache::forever(self::USER_MENU_PERMISSIONS_CACHE . $user->id, $routes);

        }

        return (array)$routes;
    }


    /**
    * 根据用户模型获取用户的菜单权限
    *
    * @param $user
    *
    * @return array|mixed
    */
   public function getUserMenusByUserModel($user)
   {


       $roles = RoleUser::query()->where('user_id',$user->id)->get()->toArray();

       $permissions = [];
       foreach ($roles as $key => $role) {
           $permissions_role=PermissionRole::query()->where('role_id',$role['role_id'])->get()->toArray();
           if(!empty($permissions_role)){
               foreach($permissions_role as $k=>$v){
                   $permissions[] = $v['permission_id'];
               }
           }
       }

       $menus_per = [];
       foreach ($permissions as $k=>$v) {
          $per=Permission::query()->where('id',$v)->get()->toArray();
          if($per[0]['type'] == 'menu'){
              $menus_per[] = $per[0]['id'];
          }
       }
       $menus=[];
       foreach ($menus_per as $menu) {
           $MenuPermission=MenuPermission::query()->where('permission_id',$menu)->get()->toArray();
           $menus[]=$MenuPermission[0];
       }

       $menus_arr=[];
       foreach($menus as $k=>$v){
           $menu_arr=Menu::query()->where('id',$v['menu_id'])->get()->toArray();
           $menu_arr[0]['menu_id']=$v['menu_id'];
           $menus_arr[]=$menu_arr[0];

       }

       return (array)$menus_arr;
   }
    /**
     * 根据用户模型获取用户的操作权限
     *
     * @param $user
     *
     * @return array|mixed
     */
    public function getUserActionPermissionsByUserModel($user)
    {
        $actions = \Cache::get(self::USER_ACTION_PERMISSIONS_CACHE . $user->id);

        if(empty($actions)){
            $roles = $user->roles;
            $permissions = [];
            foreach ($roles as $key => $role) {
                $permissions[] = $role->perms;
            }

            $actionNames = [];
            foreach ($permissions as $permission) {
                foreach ($permission as $item) {
                    if($item->type != 'action'){
                        continue;
                    }
                    $actionNames[] = $item->actions->toArray();
                }
            }

            foreach ($actionNames as $action) {
                foreach ($action as $value) {
                    $actions[] = $value['action'];
                }
            }

            if($actions){
                $actions = array_unique($actions);
            }
            \Cache::forever(self::USER_ACTION_PERMISSIONS_CACHE . $user->id, $actions);
        }

        return $actions;
    }

    /**
     * 删除缓存
     */
    public function clearCache()
    {
        foreach (static::all() as $user) {
            \Cache::forget(self::USER_MENU_PERMISSIONS_CACHE . $user->id);
            \Cache::forget(self::USER_ACTION_PERMISSIONS_CACHE . $user->id);
        }
    }
}
